﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data.SqlClient;

namespace Bug_Speed
{
    public partial class FrmTestCase : Form
    {
        public FrmTestCase()
        {
            InitializeComponent();
        }

        [Flags]
        public enum OpAttribute
        {
            ADD = 0,
            UPDATE = 1,
            DELETE = 2

        }
        
        OpAttribute op;

        public OpAttribute Operation
        {
            set { op = value; }
            get { return op; }
        }

        DataSet ds = null;

        public DataSet DS_CASE
        {
            set { ds = value; }
            get { return ds; }
        }

        private void butSave_Click(object sender, EventArgs e)
        {
            //connect to db
            try
            {
                if (op == OpAttribute.ADD)
                {
                    SqlParameter[] sqlParams = new SqlParameter[]{
                     new SqlParameter("@caseName", textBox2.Text),
                     new SqlParameter("@prodName", textBox3.Text),
                     new SqlParameter("@funName", textBox4.Text),
                     new SqlParameter("@owner", textBox5.Text),
                     new SqlParameter("@created", textBox6.Text),
                     new SqlParameter("@testPoint", textBox7.Text),
                     new SqlParameter("@testTarget", textBox8.Text),
                     new SqlParameter("@testLevel", comboBox2.SelectedValue),
                     new SqlParameter("@testType", comboBox1.SelectedValue),
                     new SqlParameter("@preCondition", textBox10.Text),
                     new SqlParameter("@testSteps", richTextBox1.Text),
                     new SqlParameter("@expectedRlt", textBox11.Text)
                    };
                    SqlHelper.ExecuteNonQuery(Properties.Settings.Default.cnnstr, CommandType.StoredProcedure, "InserTestCase", sqlParams);
                    MessageBox.Show("保存成功！");
                }
                if (op == OpAttribute.UPDATE)
                {
                    SqlParameter[] sqlParams = new SqlParameter[]{
                     new SqlParameter("@caseID",textBox1.Text),
                     new SqlParameter("@caseName", textBox2.Text),
                     new SqlParameter("@prodName", textBox3.Text),
                     new SqlParameter("@funName", textBox4.Text),
                     new SqlParameter("@owner", textBox5.Text),
                     new SqlParameter("@created", textBox6.Text),
                     new SqlParameter("@testPoint", textBox7.Text),
                     new SqlParameter("@testTarget", textBox8.Text),
                     new SqlParameter("@testLevel", comboBox2.SelectedValue),
                     new SqlParameter("@testType", comboBox1.SelectedValue),
                     new SqlParameter("@preCondition", textBox10.Text),
                     new SqlParameter("@testSteps", richTextBox1.Text),
                     new SqlParameter("@expectedRlt", textBox11.Text)
                    };
                    SqlHelper.ExecuteNonQuery(Properties.Settings.Default.cnnstr, CommandType.StoredProcedure, "updateTestCase", sqlParams);
                    MessageBox.Show("修改成功！");
                }

            }
            catch (Exception ex)
            {
                Clogs.LogError("FrmTestCase::butSave_Click Event", ex, Application.StartupPath);
                MessageBox.Show(ex.Message);
            }

        }

        private void FrmTestCase_Load(object sender, EventArgs e)
        {
            try
            {
                if (DS_CASE != null && DS_CASE.Tables[0].Rows.Count != 0)
                {
                    textBox1.Text = ds.Tables[0].Rows[0]["ID"].ToString();
                    textBox2.Text = ds.Tables[0].Rows[0]["caseName"].ToString(); //caseName
                    textBox3.Text = ds.Tables[0].Rows[0]["prodName"].ToString();//@prodName
                    textBox4.Text = ds.Tables[0].Rows[0]["funName"].ToString();//funName
                    textBox5.Text = ds.Tables[0].Rows[0]["owner"].ToString();//owner
                    textBox6.Text = ds.Tables[0].Rows[0]["created"].ToString();//created
                    textBox7.Text = ds.Tables[0].Rows[0]["testPoint"].ToString();//testPoint
                    textBox8.Text = ds.Tables[0].Rows[0]["testTarget"].ToString();//testTarget
                    comboBox2.SelectedValue = ds.Tables[0].Rows[0]["testLevel"].ToString();//testLevel
                    comboBox1.SelectedValue = ds.Tables[0].Rows[0]["testType"].ToString();//testType
                    textBox10.Text = ds.Tables[0].Rows[0]["preCondition"].ToString();//preCondition
                    richTextBox1.Text = ds.Tables[0].Rows[0]["testSteps"].ToString();//testSteps
                    textBox11.Text = ds.Tables[0].Rows[0]["expectedResult"].ToString();//expectedRlt
                }

            }
            catch (Exception ex)
            {
                Clogs.LogError("FrmTestCase::FrmTestCase_load Event", ex, Application.StartupPath);
                MessageBox.Show(ex.Message);

            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Hide();
            this.Close();
        }


    }
}
